#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;


z_Abs(x)
{
    if(x < 0)
        return 0 - x;
    
    return x;
}

z_BuildWeaponName(weapon, attach1, attach2)
{
    if(!isDefined(attach1)) {
        return (weapon + "_mp");
    }
    
    if(!isDefined(attach2)) {
        return (weapon + "_" + attach1 + "_mp");
    }
    
    return (weapon + "_" + attach1 + "_" + attach2 + "_mp");
}

z_GetCursorOrigin()
{
    eye = self getTagOrigin("tag_eye");
    curs = z_VectorScale(anglesToForward(self getPlayerAngles()), 1000000);
    return bulletTrace(eye, curs, 0, self)["position"];
}

z_GetCursorSurfaceType()
{
    eye = self getTagOrigin("tag_eye");
    curs = z_VectorScale(anglesToForward(self getPlayerAngles()), 1000000);
    return bulletTrace(eye, curs, 0, self)["surfacetype"];
}

z_GetGoodMagsCount(mag_array)
{
    count = 0;
    
    for(i = 0; i < mag_array.size; i++) {
        if(mag_array[i] > 0)
            count++;
    }
    
    return count;
}

z_GetGoodMag(cur_mag, mag_array)
{
    new_mag = randomInt(mag_array.size);
    
    while(new_mag == cur_mag || !mag_array[new_mag])
        new_mag = randomInt(mag_array.size);
    
    return new_mag;
}

z_GetGoodMag_NoInfRand(cur_mag, mag_array)
{
    new_mag = randomInt(mag_array.size);
    idx = 0;
    
    while((new_mag == cur_mag || !mag_array[new_mag]) && idx < 10) {
        new_mag = randomInt(mag_array.size);
        idx++;
    }
    
    if(mag_array[new_mag] && new_mag != cur_mag)
        return new_mag;
    
    for(idx = 0; idx < mag_array.size; idx++) {
        if(mag_array[idx] && idx != cur_mag)
            return idx;
    }
    
    return 0;
}

z_GetGLNameForWeapon(weapon)
{
    weapon_tokens = strTok(weapon, "_");
    
    switch(weapon_tokens[0]) {
        case "ak47":
            return "gl_ak47_mp";
        
        case "m16":
            return "gl_m16_mp";
        
        case "m4":
            return "gl_m4_mp";
        
        case "fn2000":
            return "gl_fn2000_mp";
        
        case "masada":
            return "gl_masada_mp";
        
        case "famas":
            return "gl_famas_mp";
        
        case "fal":
            return "gl_fal_mp";
        
        case "scar":
            return "gl_scar_mp";
        
        case "tavor":
            return "gl_tavor_mp";
        
        default:
            return "none";
    }
}

z_GetShotgunNameForWeapon(weapon)
{
    weapon_tokens = strTok(weapon, "_");
    
    switch(weapon_tokens[0]) {
        case "ak47":
            return "ak47_shotgun_attach_mp";
        
        case "m16":
            return "m16_shotgun_attach_mp";
        
        case "m4":
            return "m4_shotgun_attach_mp";
        
        case "fn2000":
            return "fn2000_shotgun_attach_mp";
        
        case "masada":
            return "masada_shotgun_attach_mp";
        
        case "famas":
            return "famas_shotgun_attach_mp";
        
        case "fal":
            return "fal_shotgun_attach_mp";
        
        case "scar":
            return "scar_shotgun_attach_mp";
        
        case "tavor":
            return "tavor_shotgun_attach_mp";
        
        default:
            return "none";
    }
}

z_GiveWeapon(weapon, mag_count, is_primary, no_give) // no_give используется для подствольных модулей
{
    self endon("death");
    self endon("disconnect");
    self endon("zclass_changed");
    self endon("zweapons_changed");
    
    weapon_clip = weaponClipSize(weapon);
    weapon_mag_num = 0; // номер заряженного магазина
    weapon_mags = [];
    for(i = 0; i < mag_count; i++) weapon_mags[i] = weapon_clip;
    
    if(!no_give) {
        self giveWeapon(weapon, 0, false);
    }
    
    if(is_primary) {
        self setSpawnWeapon(weapon);
    }
    
    self setWeaponAmmoClip(weapon, weapon_mags[weapon_mag_num]);
    self setWeaponAmmoStock(weapon, 999);
    
    weapon_in_hands = self getCurrentWeapon() == weapon;
    
    for(;;) {
        if(weapon_in_hands) { // обновляем HUD
            if(weapon_mags[weapon_mag_num] > 0) {
                self notify("zhud_wpn_upd", weapon_mags[weapon_mag_num], z_GetGoodMagsCount(weapon_mags) - 1);
            } else {
                self notify("zhud_wpn_upd", weapon_mags[weapon_mag_num], z_GetGoodMagsCount(weapon_mags));
            }
        }
        
        result = self waittill_any_return("weapon_fired", "reload", "weapon_change");
        
        if(weapon_in_hands && result == "weapon_fired") {
            weapon_mags[weapon_mag_num]--;
        } else if(weapon_in_hands && result == "reload") {
            mags_left = z_GetGoodMagsCount(weapon_mags);
            
            if(mags_left > 1) {
                weapon_mag_num = z_GetGoodMag(weapon_mag_num, weapon_mags);
                
                self setWeaponAmmoClip(weapon, weapon_mags[weapon_mag_num]);
                self setWeaponAmmoStock(weapon, 999);
            } else if(mags_left == 1) {
                weapon_mag_num = z_GetGoodMag(weapon_mag_num, weapon_mags);
                
                self setWeaponAmmoClip(weapon, weapon_mags[weapon_mag_num]);
                self setWeaponAmmoStock(weapon, 0);
            } else {
                self setWeaponAmmoClip(weapon, 0);
                self setWeaponAmmoStock(weapon, 0);
            }
        } else if(result == "weapon_change") {
            weapon_in_hands = self getCurrentWeapon() == weapon;
        }
    }
}

z_GiveWeaponAlt(weapon, ammo, is_primary, no_give) // для оружия с подзарядкой (spas12, striker, m1014, mounted shotgun, etc)
{
    self endon("death");
    self endon("disconnect");
    self endon("zclass_changed");
    self endon("zweapons_changed");
    
    weapon_clip_size = weaponClipSize(weapon);
    
    weapon_clip = weapon_clip_size; // кол-во заряженных патронов
    weapon_stock = ammo - weapon_clip; // кол-во запасных патронов
    
    if(!no_give) {
        self giveWeapon(weapon, 0, false);
    }
    
    if(is_primary) {
        self setSpawnWeapon(weapon);
    }
    
    self setWeaponAmmoClip(weapon, weapon_clip);
    self setWeaponAmmoStock(weapon, weapon_stock);
    
    weapon_in_hands = self getCurrentWeapon() == weapon;
    
    for(;;) {
        if(weapon_in_hands) { // обновляем HUD
            self notify("zhud_wpn_upd", weapon_clip, weapon_stock);
        }
        
        result = self waittill_any_return("weapon_fired", "reload", "weapon_change");
        
        if(weapon_in_hands && result == "weapon_fired") {
            weapon_clip--;
        } else if(weapon_in_hands && result == "reload") {
            weapon_clip++;
            weapon_stock--;
            
            self setWeaponAmmoStock(weapon, weapon_stock);
        } else if(result == "weapon_change") {
            weapon_in_hands = self getCurrentWeapon() == weapon;
        }
    }
}

z_GiveWeapons()
{
    primary_weapon_class = getWeaponClass(self.zClass_PrimaryWeapon);
    
    if(primary_weapon_class == "weapon_shotgun" && !string_starts_with(self.zClass_PrimaryWeapon, "aa12")) {
        self thread z_GiveWeaponAlt(self.zClass_PrimaryWeapon, 40, true, false);
    } else if(primary_weapon_class == "weapon_lmg" && !string_starts_with(self.zClass_PrimaryWeapon, "aug")) {
        self thread z_GiveWeapon(self.zClass_PrimaryWeapon, 5, true, false);
    } else {
        self thread z_GiveWeapon(self.zClass_PrimaryWeapon, 10, true, false);
    }
    
    if(isSubStr(self.zClass_PrimaryWeapon, "gl")) {
        self thread z_GiveWeapon(z_GetGLNameForWeapon(self.zClass_PrimaryWeapon), 6, false, true);
    } else if(isSubStr(self.zClass_PrimaryWeapon, "shotgun_attach")) {
        self thread z_GiveWeaponAlt(z_GetShotgunNameForWeapon(self.zClass_PrimaryWeapon), 25, false, true);
    }
    
    self thread z_GiveWeapon(self.zClass_SecondaryWeapon, 4, false, false);
}

z_VectorScale(vec, scale)
{
    return (vec[0] * scale, vec[1] * scale, vec[2] * scale);
}

z_WeaponModelManageTags(weapon, extratag0, extratag1, extratag2, extratag3, extratag4)
{
    /// WEAPON TAGS:
    /// "tag_silencer"
    /// "tag_red_dot"
    /// "tag_tavor_scope"
    /// "tag_thermal_scope"
    /// "tag_eotech"
    /// "tag_fn2000_scope"
    /// "tag_rail"
    /// "tag_rear_sight"
    /// "tag_iron_sight"
    /// "tag_sight_on"
    /// "tag_m203"
    /// "tag_gp25"
    /// "tag_shotgun"
    /// "tag_acog_2"
    /// "tag_sa80_scope"
    /// "tag_acog"
    /// "tag_steyr_scope"
    /// "tag_scope"
    /// "tag_m14ebr_scope"
    /// "tag_m82_scope"
    /// "tag_wa2000_scope"
    /// "tag_cheytac_scope"
    /// "tag_heartbeat"
    /// "tag_foregrip"
    
    // получаем массив с лишними тегами
    hideTagList = getWeaponHideTags(weapon);
    
    if(!isDefined(hideTagList))
        return;
    
    // скрываем лишние теги
    for(i = 0; i < hideTagList.size; i++)
        self hidePart(hideTagList[i]);
    
    // добавляем дополнительные теги
    if(isDefined(extratag0)) {
        self showPart(extratag0);
        
        if(isDefined(extratag1)) {
            self showPart(extratag1);
            
            if(isDefined(extratag2)) {
                self showPart(extratag2);
                
                if(isDefined(extratag3)) {
                    self showPart(extratag3);
                    
                    if(isDefined(extratag4)) {
                        self showPart(extratag4);
                        
                        // <...>
                    }
                }
            }
        }
    }
}

